package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.behavior.entity.ApUnlikesBehavior;
import com.usian.behavior.mapper.ApUnlikesBehaviorMapper;
import com.usian.behavior.service.IApUnlikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.threadlocal.UserThreadLocalUtils;
import com.usian.model.behavior.dtos.UnLikesBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Override
    public void unLike(UnLikesBehaviorDto dto) {
        //1、参数校验
        if(dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2、获取用户id，判断用户是否登录
        boolean isUser = false;
        Integer userId = UserThreadLocalUtils.getUserId();
        if(userId!=null && userId!=0){
            //用户是登录状态
            isUser=true;
        }

        if(userId==null && dto.getEquipmentId()==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE,"用户id和设备id不能同时为空");
        }

        //3、查询用户或设备针对当前文章是否已经点赞
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        if(isUser){
            //登录状态，根据用户id查询
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId,userId);
        }else{
            //未登录状态，根据设备号查询
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId,dto.getEquipmentId());
        }

        ApUnlikesBehavior unlikesBehavior = this.getOne(queryWrapper);
        if(unlikesBehavior==null){
            //3.1 如果未不喜欢文章，记录不喜欢行为
            unlikesBehavior = new ApUnlikesBehavior();
            if(isUser){
                unlikesBehavior.setApUserId(userId);
            }else{
                unlikesBehavior.setEquipmentId(dto.getEquipmentId());
            }
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setType(dto.getType());
            unlikesBehavior.setCreatedTime(new Date());
            unlikesBehavior.setUpdateTime(new Date());
            boolean b = this.save(unlikesBehavior);
        }else{
            //3.2 如果已经不喜欢，更新不喜欢状态
            unlikesBehavior.setType(dto.getType());
            unlikesBehavior.setUpdateTime(new Date());
            updateById(unlikesBehavior);
        }
    }

    @Override
    public ApUnlikesBehavior findUnLikeByArticleId(Integer userId, String equipmentId, Long articleId) {
        //查询用户或者设备号查询不喜欢行为
        boolean isUser = false;
        if (userId != null && userId != 0) {
            isUser = true;
        }
        //查询用户或者设备是否对当前不喜欢进行点赞
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId, articleId);
        if (isUser) {
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId, userId);
        } else {
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId, equipmentId);
        }
        ApUnlikesBehavior apUnlikesBehavior = this.getOne(queryWrapper);

        return apUnlikesBehavior;
    }
}
